package com.example.smartf.repository.mysql;

import com.example.smartf.model.mysql.Deviceinfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface DeviceinfoRepository extends JpaRepository<Deviceinfo,String> {
    //通过deviceid查找设备
    Deviceinfo findByDeviceid(String deviceid);

    //查询所有的device中的deviceid
    @Transactional
    @Modifying
    @Query("select deviceid from Deviceinfo")
    List<String> findAlldeviceid();

    //查询所有的正在使用的device中的deviceid
    @Transactional
    @Modifying
    @Query("select deviceid from Deviceinfo where startday is not null")
    List<String> findAlldeviceidinUsed();

    //通过deviceID更新 起始时间
    @Transactional
    @Modifying
    @Query("update Deviceinfo set startday = :startDay where deviceid = :deviceID")
    int changeStartDay(String deviceID , String startDay);


    //通过deviceID更新 设备名
    @Transactional
    @Modifying
    @Query("update Deviceinfo set devicename = :deviceName where deviceid = :deviceID")
    int changedevicename(String deviceID , String deviceName);

    //通过deviceID更新 设备备注
    @Transactional
    @Modifying
    @Query("update Deviceinfo set remarks = :reMarks where deviceid = :deviceID")
    int changeremarks(String deviceID , String reMarks);

    //通过deviceID更新 三个值：起始时间，设备名，备注
    @Transactional
    @Modifying
    @Query("update Deviceinfo set kind = :kind , startday = :startDay , devicename = :deviceName , remarks = :reMarks where deviceid = :deviceID")
    int changedeviceExtrainfo(String deviceID , String kind ,String startDay, String deviceName , String reMarks);

    //通过deviceID 更新 设备的co2阈值
    @Transactional
    @Modifying
    @Query("update Deviceinfo set co2min = :co2min , co2max = :co2max where deviceid = :deviceID")
    int updateCO2threshold(String deviceID, Double co2max, Double co2min);

    //通过deviceID 更新 设备的ec阈值
    @Transactional
    @Modifying
    @Query("update Deviceinfo set ecmin = :ecmin , ecmax = :ecmax where deviceid = :deviceID")
    int updateECthreshold(String deviceID, Double ecmax, Double ecmin);

    @Transactional
    @Modifying
    @Query("update Deviceinfo set lightmin = :lightmin , lightmax = :lightmax where deviceid = :deviceID")
    int updateLIGHTthreshold(String deviceID, Double lightmax, Double lightmin);

    //通过deviceID 更新 设备的ph阈值
    @Transactional
    @Modifying
    @Query("update Deviceinfo set phmin = :phmin , phmax = :phmax where deviceid = :deviceID")
    int updatePHthreshold(String deviceID, Double phmax, Double phmin);

    //通过deviceID 更新 设备的rh阈值
    @Transactional
    @Modifying
    @Query("update Deviceinfo set rhmin = :rhmin , rhmax = :rhmax where deviceid = :deviceID")
    int updateRHthreshold(String deviceID, Double rhmax, Double rhmin);

    //通过deviceID 更新 设备的temp阈值
    @Transactional
    @Modifying
    @Query("update Deviceinfo set tempmin = :tempmin , tempmax = :tempmax where deviceid = :deviceID")
    int updateTEMPthreshold(String deviceID, Double tempmax, Double tempmin);
}
